package labb3;

import javax.swing.*;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

/**
 * Make it possible to create a JMenuBar with all the needed items.
 *
 * @author Mikko Puustinen
 * @version 0.1, 2007-okt-14
 */
class Menu {
    private final GUI gui;
    private final String file = "File";
    private final String openCollection = "Open collection";
    private final String saveCollection = "Save";
    private final String saveAsCollection = "Save As";
    private final String exit = "Exit";
    private final String dvd = "DVD";
    private final String addMovie = "Add";
    private final String editMovie = "Edit";
    private final String deleteMovie = "Delete";
    private final String help = "Help";
    private final String about = "About";
    private JTextField searchField;

    /**
     * Makes it possible to later on create a menu.
     *
     * @param gui reference to the GUI.
     */
    public Menu(GUI gui) {
        this.gui = gui;
    }

    /**
     * Constructs a standard menu.
     *
     * @return the MenuBar.
     *
     * @see javax.swing.JMenuBar
     * @see javax.swing.JMenu
     * @see javax.swing.JMenuItem
     */
    public JMenuBar constructMenuBar() {
        JMenu fileMenu;
        JMenu editMenu;
        JMenu helpMenu;
        JMenuBar menuBar;
        JMenuItem menuItem;
        MenuItemActionListener listener = new MenuItemActionListener();

        menuBar = new JMenuBar();

        fileMenu = new JMenu(file);
        fileMenu.setMnemonic(KeyEvent.VK_F);
        menuBar.add(fileMenu);

        menuItem = new JMenuItem(openCollection, KeyEvent.VK_O);
        menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK));
        menuItem.addActionListener(listener);
        fileMenu.add(menuItem);

        menuItem = new JMenuItem(saveCollection, KeyEvent.VK_S);
        menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));
        menuItem.addActionListener(listener);
        fileMenu.add(menuItem);

        menuItem = new JMenuItem(saveAsCollection, KeyEvent.VK_A);
        menuItem.addActionListener(listener);
        fileMenu.add(menuItem);

        menuItem = new JMenuItem(exit, KeyEvent.VK_X);
        menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F4, ActionEvent.ALT_MASK));
        menuItem.addActionListener(listener);
        fileMenu.add(menuItem);

        editMenu = new JMenu(dvd);
        editMenu.setMnemonic(KeyEvent.VK_D);
        menuBar.add(editMenu);

        menuItem = new JMenuItem(addMovie, KeyEvent.VK_A);
        menuItem.addActionListener(listener);
        editMenu.add(menuItem);

        menuItem = new JMenuItem(editMovie, KeyEvent.VK_E);
        menuItem.addActionListener(listener);
        editMenu.add(menuItem);

        menuItem = new JMenuItem(deleteMovie, KeyEvent.VK_D);
        menuItem.addActionListener(listener);
        editMenu.add(menuItem);

        helpMenu = new JMenu(help);
        menuBar.add(helpMenu);

        menuItem = new JMenuItem(about);
        menuItem.addActionListener(listener);
        helpMenu.add(menuItem);

        menuBar.add(Box.createHorizontalStrut(250));
        JLabel searchLabel = new JLabel("Filter:");
        searchLabel.setFont(new Font(Font.DIALOG, Font.PLAIN, 10));

        searchField = new JTextField();
        searchLabel.setLabelFor(searchField);
        searchField.setBackground(Color.WHITE);
        searchField.setBorder(BorderFactory.createBevelBorder(1));
        searchField.addKeyListener(new SearchFieldKeyListener());
        menuBar.add(searchLabel);
        menuBar.add(searchField);
        menuBar.add(Box.createHorizontalStrut(5));

        //TODO: create own classes for all listeners, example: AddMovieListener(Controller)?
        return menuBar;
    }

    /**
     * Creates an ActionListener
     */
    private class MenuItemActionListener implements ActionListener {
        /**
         * Invoked when an action occurs.
         *
         * @param e event.
         */
        public void actionPerformed(ActionEvent e) {
            if (e.getActionCommand().equals(openCollection)) {
                gui.open();
            } else if (e.getActionCommand().equals(saveCollection)) {
                gui.save();
            } else if (e.getActionCommand().equals(saveAsCollection)) {
                gui.saveAs();
            } else if (e.getActionCommand().equals(exit)) {
                gui.exitProgram();
            } else if (e.getActionCommand().equals(addMovie)) {
                gui.addMedia();
            } else if (e.getActionCommand().equals(editMovie)) {
                gui.editMedia();
            } else if (e.getActionCommand().equals(deleteMovie)) {
                gui.removeMedia();
            } else if (e.getActionCommand().equals(about)) {
                gui.showAbout();
            }
            //TODO: Maybe use some external class to handle all the actions instead of GUI taking care of it all
        }
    }

    /**
     * Creates a listener for
     */
    private class SearchFieldKeyListener extends KeyAdapter {
        /**
         * Invoked when a key has been released. See the class description for
         * {@link java.awt.event.KeyEvent} for a definition of a key released
         * event.
         */
        public void keyReleased(KeyEvent e) {
            int key = e.getKeyCode();
            if (Character.isLetterOrDigit(e.getKeyChar()) || key == 8) {
                String searchFor = searchField.getText();
                gui.search(searchFor);
            }
        }
    }
}
